package cc.comac.mom.datahandlers.entity;

import java.time.LocalDate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cc.comac.mom.dao.entity.UniversalNumberGenerationDao;
import cc.comac.mom.entity.UniversalNumberGenerationEntity;
import cc.comac.mom.util.BaseEntityDataUtil;

@Component
@Transactional
public class UniversalNumberGenerationDataHandler {

	@Autowired
	private UniversalNumberGenerationDao universalNumberGenerationDao;
	
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	/**
	 * 入库单前缀
	 * **/
	public final String inWarehouseNumberGeneration = "RK";
	
	/**
	 * 盘点前缀
	 * **/
	public final String inventoryNumberGeneration = "BJPD";
	
	/**
	 * 领用单前缀
	 * **/
	public final String borrowNumberGeneration = "LY";
	
	/**
	 * 保养计划前缀
	 * **/
	public final String maintainPlan = "BYJH";
	
	/**
	 * 保养工单前缀
	 * **/
	public final String maintainWork = "BYGD";
	
	/**
	 * 入库,领用,盘点编码生成
	 * **/
	public String numberGeneration (String pre) {
		String  prefix = pre;
		LocalDate localTime = LocalDate.now();
		QueryWrapper<UniversalNumberGenerationEntity> queryWrapper = new QueryWrapper<UniversalNumberGenerationEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("prefix", prefix);
		queryWrapper.eq("current_date", localTime);
		UniversalNumberGenerationEntity selectOne = universalNumberGenerationDao.selectOne(queryWrapper);
		if (selectOne == null) {
			UniversalNumberGenerationEntity universalNumberGenerationEntity = new UniversalNumberGenerationEntity();
			universalNumberGenerationEntity.setPrefix(prefix);
			universalNumberGenerationEntity.setEncodingDate(localTime);
			universalNumberGenerationEntity.setSuffix("1");
			universalNumberGenerationEntity.setRowStatus(1);
			baseEntityDataUtil.setBaseEntityData(universalNumberGenerationEntity);
			universalNumberGenerationDao.insert(universalNumberGenerationEntity);
			return prefix + baseEntityDataUtil.getStringTime()+"000001";
		} else {
			int suffix = Integer.parseInt(selectOne.getSuffix());
			suffix = suffix + 1;
			// 向前补零
			 // 0 代表前面补充0 ,6 代表长度为6 , d 代表参数为正数型       
			String format = String.format("%06d", suffix);
			selectOne.setSuffix(suffix+"");
			baseEntityDataUtil.updateBaseEntityData(selectOne);
			universalNumberGenerationDao.updateById(selectOne);
			return prefix + baseEntityDataUtil.getStringTime()+format;
		}
	}
	public String getInWarehouseNumberGeneration() {
		return inWarehouseNumberGeneration;
	}
	public String getInventoryNumberGeneration() {
		return inventoryNumberGeneration;
	}
	public String getBorrowNumberGeneration() {
		return borrowNumberGeneration;
	}
	public String getMaintainPlan() {
		return maintainPlan;
	}
	public String getMaintainWork() {
		return maintainWork;
	}
	
	
}
